//
//  MuseRefreshTool.swift
//  Muse
//
//  Created by mac on 2022/12/20.
//

import UIKit
import CRRefresh

extension UIScrollView {
    
    // 添加刷新
    @discardableResult
    func refresh_pull(refreshBlock: (() -> Void)?) -> Self {
        self.cr.addHeadRefresh(animator: FastAnimator()) { [weak self] in
            self?.resetNoMoreInfo()
            refreshBlock?()
        }
        return self
    }
    
    @discardableResult
    func refresh_loadMore(refreshBlock: @escaping (() -> Void)) -> Self {
        self.cr.addFootRefresh(animator: NormalFooterAnimator(), handler: refreshBlock)
        return self
    }
    
    /// 默认触发下拉刷新(上拉加载极少经过手动触发)
    @discardableResult
    func refresh_start() -> Self {
        self.cr.beginHeaderRefresh()
        return resetNoMoreInfo()
    }
    
    /// 结束刷新动画)
    @discardableResult
    func refresh_end(_ isNoMoreData: Bool = false) -> Self {
        // 结束下拉动画
        self.cr.endHeaderRefresh()
        // 结束上拉动画
        self.cr.endLoadingMore()
        
        if let view = self.viewWithTag(79898784784) {
            view.removeFromSuperview()
        }
        if isNoMoreData {
            /// 没有更多数据
            self.cr.noticeNoMoreData()
            // 添加没有更多数据提示
            let footerView = MuseTipsView(self).rae
                .width(self.frame.width).height(50.0)
                .tag(79898784784).end()
            self.addSubview(footerView)
        }
        return self
    }
    
    /// 重置更多数据
    @discardableResult
    func resetNoMoreInfo() -> Self {
        self.cr.resetNoMore()
        return self
    }
    
    /// 移除下拉刷新
    func refresh_removePull() {
        self.cr.removeHeader()
    }
    
}

fileprivate class MuseTipsView: UIView {
    
    private var scrollView: UIScrollView?
    
    private lazy var tipsLabel = c_make_ex(UILabel.self)
        .bgColor(.clear).color("#c4c4c4")
        .font(16.font_medium)
        .alignment(.center).end()
    
    private var observation: NSKeyValueObservation?
    
    init(_ scrollView: UIScrollView) {
        super.init(frame: .zero)
        self.scrollView = scrollView
        subviews(tipsLabel)
        tipsLabel.fillContainer()
        
        observation = scrollView.observe(\.contentSize, options: .new, changeHandler: { [weak self] _, change in
            guard change.newValue != change.oldValue else { return }
            let content_height = change.newValue?.height ?? 0
            var frame = self?.frame ?? .zero
            frame.origin.y = content_height - frame.height
            self?.frame = frame
            
            self?.tipsLabel.isHidden = !(content_height > 100)
        })
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        var frame = self.frame
        frame.origin.y = scrollView?.contentSize.height ?? 0 - frame.height
        self.frame = frame
        
        self.tipsLabel.isHidden = !(scrollView?.contentSize.height ?? 0 > 100)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
